package cn.htsyb.synergy.security.entity;

import cn.htsyb.synergy.orm.jpa.model.entity.AbsStandardEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import java.util.List;

/**
 * 角色 实体类
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @date 2022/5/23
 * @since 1.0
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Setter
@Getter
@NoArgsConstructor
@Entity
@DynamicInsert
@Table(name = "security_role")
@org.hibernate.annotations.Table(appliesTo = "security_role", comment = "角色")
@Schema(description = "角色")
public class SecurityRole extends AbsStandardEntity {
    private static final long serialVersionUID = -781003841468228072L;

    /**
     * 角色名称
     */
    @Column(name = "role_name", nullable = false, length = 50, columnDefinition = " VARCHAR(50) COMMENT '角色名称' ")
    @NotNull
    @Schema(name = "roleName", description = "角色名称", required = true)
    protected String roleName;
    /**
     * 角色编码
     */
    @Column(name = "role_code", length = 50, columnDefinition = " VARCHAR(50) COMMENT '角色编码' ")
    @Schema(name = "roleCode", description = "角色编码")
    protected String roleCode;
    /**
     * 拥有的用户集合
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    //如果使用mappedBy 不使用@JoinTable,则代表本地不维护关系
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = SecurityUser.class)
    //@ManyToMany(fetch = FetchType.LAZY, targetEntity = SecurityUser.class, mappedBy = "roles")
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinTable(name = "security_role_user",
            uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "role_id"})},
            inverseJoinColumns = @JoinColumn(name = "user_id", nullable = false, updatable = false, insertable = false,
                    foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT),
                    columnDefinition = "varchar(36) COMMENT '用户'"),
            joinColumns = @JoinColumn(name = "role_id", nullable = false, updatable = false, insertable = false,
                    foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT),
                    columnDefinition = "varchar(36) COMMENT '角色'"))
    protected List<SecurityUser> users;
    /**
     * 授权的资源
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @ManyToMany(fetch = FetchType.LAZY, /*cascade = CascadeType.ALL, //会关联插入子对象*/  targetEntity = SecurityPermission.class)
    @NotFound(action = NotFoundAction.IGNORE)//允许映射对象为空
    @JoinTable(name = "security_role_authorize",
            uniqueConstraints = {@UniqueConstraint(columnNames = {"permission_id", "role_id"})},
            joinColumns = @JoinColumn(name = "role_id", updatable = false, insertable = false, nullable = false,
                    foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT),
                    columnDefinition = "varchar(36) COMMENT '角色'"),
            inverseJoinColumns = @JoinColumn(name = "permission_id", updatable = false, insertable = false, nullable = false,
                    foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT),
                    columnDefinition = "varchar(36) COMMENT '权限'"))
    protected List<SecurityPermission> permissions;
}